package org.item.customer.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.item.common.bean.CustomerContacts;
import org.item.common.service.ICustomerContactsService;
import org.item.customer.mapper.CustomerContactsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomerContactsService implements ICustomerContactsService {
    @Autowired
    CustomerContactsMapper customerContactsMapper;

    @Override
    public CustomerContacts insert(CustomerContacts customerContacts) {
        customerContactsMapper.insert(customerContacts);
        CustomerContacts result=customerContactsMapper.selectById(customerContacts.getId());
        return result;
    }

    @Override
    public CustomerContacts update(CustomerContacts customerContacts) {
        customerContactsMapper.updateById(customerContacts);
        CustomerContacts result=customerContactsMapper.selectById(customerContacts.getId());
        return result;
    }

    @Override
    public int delete(Integer id) {
        int num=customerContactsMapper.deleteById(id);;
        return num;
    }

    @Override
    public int setPrimary(Integer id) {
        UpdateWrapper wrapper=new UpdateWrapper();
        CustomerContacts customerContacts=customerContactsMapper.selectById(id);
        if (customerContacts.getIs_primary()==0){
            wrapper.set("is_primary",1);
        }else {
            wrapper.set("is_primary",0);
        }
        wrapper.eq("id",id);
        int result=customerContactsMapper.update(wrapper);
        return result;
    }
}
